<?php 
class control_home extends control  {

	public function dotest()
	{
		$url=get_url('home/init',array('p'=>2));
		echo 'hello word';
	}
	
	public function doinit()
	{
		//debug('b');
		//$class=new lib_db_test_newfile();
		//debug($this->module_adminuser());
		view::render();
	}
	
	/**
	 * 
	 * 系统检查
	 */
	public function dostep1()
	{
		$state=0;
		/**
		 * 
		 * php 版本检查
		 * @var bool
		 */
		$php_version=FALSE;
		if(version_compare(PHP_VERSION,'5.2','>='))
		{
			$php_version=TRUE;
			$state+=1;
		}
		
		/**
		 * 
		 * spl扩展检查
		 * @var bool
		 */
		$spl_enabled=FALSE;
		if(function_exists('spl_autoload_register'))
		{
			$spl_enabled=true;
			$state+=1;
		}
		
		/**
		 * 
		 * mysqli扩展检查
		 * @var bool
		 */
		$mysqli_enabled=FALSE;
		if(function_exists('mysqli_connect'))
		{
			$mysqli_enabled=TRUE;
			$state+=1;
		}
		
		/**
		 * PDO扩展检查
		 * @var bool
		 */
		$pdo_enabled=FALSE;
		if(extension_loaded('pdo'))
		{
			$pdo_enabled=TRUE;
			$state+=1;
		}
		
		/**
		 * 
		 * PDO mysql 展检查
		 * @var bool
		 */
		$pdomysql_enabled=FALSE;
		if(extension_loaded('pdo_mysql'))
		{
			$pdomysql_enabled=TRUE;
			$state+=1;
		}
		
		/**
		 * 
		 * 上传目录写权限检查
		 * @var bool
		 */
		$upload_enabled=FALSE;
		if(is_writable(HZHIHU_ROOT.$this->config->upload_dir))
		{
			$upload_enabled=TRUE;
			$state+=1;
		}
		
		/**
		 * 
		 * SESSION存储目录写权限检查
		 * @var bool
		 */
		$session_enabled=TRUE;
		if($this->config->sessionsavepath && !is_writable(HZHIHU_ROOT.$this->config->sessionsavepath))
		{
			$session_enabled=FALSE;
			$state-=1;
		}else{
			$state+=1;
		}
		
		
		/**
		 * 
		 * 配置目录临时开启写检查
		 * @var unknown_type
		 */
		$config_tmp_enabled=FALSE;
		if(is_writable(HZHIHU_ROOT.'config/'))
		{
			$config_tmp_enabled=TRUE;
			$state+=1;
		}
		
		$state=$state<8?FALSE:TRUE;
		view::set('submit', $state);
		view::set('pdomysql_enabled', $pdomysql_enabled);
		view::set('config_tmp_enabled', $config_tmp_enabled);
		view::set('session_enabled', $session_enabled);
		view::set('upload_enabled', $upload_enabled);
		view::set('mysqli_enabled', $mysqli_enabled);
		view::set('pdo_enabled', $pdo_enabled);
		view::set('spl_enabled', $spl_enabled);
		view::set('php_version', $php_version);
		view::render();
	}
	
	public function dochecksql_connet()
	{
		ob_start();
		try {
			$link = mysql_connect($this->requests->post['dbHost'].':'.$this->requests->post['dbPort'], $this->requests->post['dbUser'], 
									$this->requests->post['dbPassword']);
			$string=ob_get_contents();
			ob_end_clean();
			if (!$link) {
				exit(json_encode(array("inputid"=>'sql_error','msg'=>sprintf("Connect failed: %s\n",$string),'success'=>0)));
			}
				
			mysql_select_db($this->requests->post['dbName'],$link) or die(json_encode(array("inputid"=>'sql_error','msg'=>sprintf("Connect failed: %s\n",mysql_error()),'success'=>0)));
			
			exit(json_encode(array('success'=>1)));			
		} catch (Exception $e) {
			exit(json_encode(array("inputid"=>'sql_error','msg'=>sprintf("Connect failed: %s\n",mysql_error()),'success'=>1)));
		}
	}
	
	public function dostep2()
	{
		$ajax_url=get_url('home/checksql_connet');
		view::set('ajax_checksql',$ajax_url);
		
	    if($this->requests->server['REQUEST_METHOD']=='POST')
	    {
$String= <<<END
<?php
/**
 * 
 *
 * @copyright	Copyright 2009-2010   我的，也是你的,PHP生活
 * @license	LGPL (http://www.gnu.org/licenses/lgpl.html)
 * @author	XinHui Wang <wangxinhui@fanhougame.com>
 * @package   配置文件
 * @version	2867
 * @link		http://wxh.limewebs.com/
 */
 
return array(
				'installed'=>false,
				'debug'=>false,
				'webRoot'=>'{$this->requests->post["webRoot"]}',
				'language'=>'{$this->requests->post['defaultLang']}',
				'htaccess'=>{$this->requests->post['requestType']},
				'db'=>array('default'=>array(
        									  'driver'=>'mysqli', //数据库连接引擎
        									  'db_host'=>'{$this->requests->post['dbHost']}',//host
        									  'db_port'=>{$this->requests->post['dbPort']},//端口
        									  'db_username'=>'{$this->requests->post['dbUser']}',//数据库用户名
        									  'db_password'=>'{$this->requests->post['dbPassword']}',//数据库密码
        									  'db_charset'=>'{$this->requests->post['dbCharset']}',
        									  'db_name'=>'{$this->requests->post['dbName']}',
        									  'db_prefix'=>'{$this->requests->post['dbPrefix']}',
									))
					
	);	              
END;

$Enter=<<<END
<?php
define('IN_HZHIU', true); //程序入口
define('CONTROL', '{$this->requests->post['mvcdir']}');
require_once '../common.inc.php';
define('CONTROL_FILE','{$this->requests->post['mvcdir']}'.DIRECTORY_SEPARATOR.'control'.DIRECTORY_SEPARATOR);  #定義控制層目錄
define('MODULE_FILE','{$this->requests->post['mvcdir']}'.DIRECTORY_SEPARATOR.'module'.DIRECTORY_SEPARATOR);    #定義數據層目錄；
define('VIEW_FILE','{$this->requests->post['mvcdir']}'.DIRECTORY_SEPARATOR.'view'.DIRECTORY_SEPARATOR);         #定义显示层目录
define('LANGUAGE_FILE','{$this->requests->post['mvcdir']}'.DIRECTORY_SEPARATOR.'language'.DIRECTORY_SEPARATOR);  #定义語言包
%MYSQLI_CLASS%%Sql_CLASS%%Table_CLASS%%Base_CLASS%%Control_CLASS%%Module_CLASS%%REQUEST_CLASS%%CONFIGS_CLASS%%R_CLASS%%COMMON.FUN%%LANGUAGE_CLASS%
%DEBUG%
/**
 * 加载类
 */
R::implode(HZHIHU_ROOT.'config'.HZHIHU_DIREND);
R::implode(HZHIHU_ROOT."lib".HZHIHU_DIREND);
R::implode(HZHIHU_ROOT."helpers".HZHIHU_DIREND);
\$con=new R();
\$con->Run();

END;

		  /**
		   * 把主文件,读出写入指定文件
		   */		   
         FileSystem::writefile(HZHIHU_ROOT.'config'.HZHIHU_DIREND.'myconfig.php', $String);
         $R_string=FileSystem::removeSurplus(HZHIHU_ROOT.'lib'.HZHIHU_DIREND.'R.php');
         $Base_string=FileSystem::removeSurplus(HZHIHU_ROOT.'lib'.HZHIHU_DIREND.'base'.HZHIHU_DIREND.'base.php');
         $com_inc_string=FileSystem::removeSurplus(HZHIHU_ROOT.'common.inc.php');
         $com_fun_string=FileSystem::removeSurplus(HZHIHU_ROOT.'common.fun.php');
         $control_string=FileSystem::removeSurplus(HZHIHU_ROOT.'lib'.HZHIHU_DIREND.'control.php');
         $module_string=FileSystem::removeSurplus(HZHIHU_ROOT.'lib'.HZHIHU_DIREND.'module.php');
         $request_string=FileSystem::removeSurplus(HZHIHU_ROOT.'lib'.HZHIHU_DIREND.'request.php');
         $configs_string=FileSystem::removeSurplus(HZHIHU_ROOT.'lib'.HZHIHU_DIREND.'configs.php');
         $table_string=FileSystem::removeSurplus(HZHIHU_ROOT.'lib'.HZHIHU_DIREND.'db'.HZHIHU_DIREND.'table.php');
         $factory_string=FileSystem::removeSurplus(HZHIHU_ROOT.'lib'.HZHIHU_DIREND.'db'.HZHIHU_DIREND.'factory.php');
         $mysqli_string=FileSystem::removeSurplus(HZHIHU_ROOT.'lib'.HZHIHU_DIREND.'db'.HZHIHU_DIREND.'mysqli.php');
         $sql_string=FileSystem::removeSurplus(HZHIHU_ROOT.'lib'.HZHIHU_DIREND.'db'.HZHIHU_DIREND.'sql.php');
         $debug_string=FileSystem::removeSurplus(HZHIHU_ROOT.'lib'.HZHIHU_DIREND.'debug'.HZHIHU_DIREND.'debug.php');
         $language_string=FileSystem::removeSurplus(HZHIHU_ROOT.'lib'.HZHIHU_DIREND.HZHIHU_DIREND.'language.php');
         
         $searchAry=array('%Base_CLASS%',
         				  '%R_CLASS%',
         				  '%COMMON.INC%',
         				  '%COMMON.FUN%',
         				  '%Module_CLASS%',
         				  '%Control_CLASS%',
         				  '%Table_CLASS%',
         				  '%Sql_CLASS%',
         				  '%DEBUG%',
         				  '%LANGUAGE_CLASS%',
         				  '%FACTORY_CLASS%',
         				  '%MYSQLI_CLASS%',
         				  '%REQUEST_CLASS%',
         				  '%CONFIGS_CLASS%',
         			);
         $replaceAry=array($Base_string,
         				   $R_string,
         				   $com_inc_string,
         				   $com_fun_string,
         				   $module_string,
         				   $control_string,
         				   $table_string,
         				   $sql_string,
         				   $debug_string,
         				   $language_string,
         				   $factory_string,
         				   $mysqli_string,
         				   $request_string,
         				   $configs_string
         				   );    
         $R_string=str_replace($searchAry, $replaceAry, $Enter);
         FileSystem::writefile(HZHIHU_ROOT.$this->config->www_file.$this->requests->post['inFile'], $R_string);
         unset($Enter,$R_string);
		  gourl($this->requests->post['inFile']);
	    }
	    

        $action=get_url('home/step2',array('s'=>'post'));
        view::set('action', $action);
        view::set('config', $this->config);
        view::render();
	}
	
	
}
?>